package com.scujj.cs6.controller;

import com.scujj.cs6.common.Result;
import com.scujj.cs6.model.vo.*;
import com.scujj.cs6.service.WeatherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "天气接口", description = "天气查询")
@RestController
@RequestMapping("/weather")
@RequiredArgsConstructor
public class WeatherController {

    private final WeatherService weatherService;

    @GetMapping("/")
    @Operation(summary = "通过IP获取当地天气信息与当地区划信息", description = "根据IP地址获取本地天气所有信息")
    @ApiResponse(responseCode = "200", description = "成功获取天气信息",
            content = @Content(mediaType = "application/json",
                schema = @Schema(implementation = WeatherAllVo.class)
            )
    )
    public Result<WeatherAllVo> getWeather(HttpServletRequest request) {
        return Result.success(weatherService.fetchWeatherByIp(request));
    }

    @GetMapping("/adcode/{adcode}")
    @Operation(summary = "通过adcode获取当地天气信息与当地区划信息", description = "根据IP地址获取本地天气所有信息")
    @ApiResponse(responseCode = "200", description = "成功获取天气信息",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = WeatherAllVo.class)
            )
    )
    public Result<WeatherAllVo> getWeatherByAdcode(@PathVariable("adcode") @Parameter(
            description = "行政区划代码",
            example = "440100",
            required = true,
            name = "adcode",
            in = ParameterIn.PATH
    ) String adcode) {
        return Result.success(weatherService.fetchWeatherByAdcode(adcode));
    }

    @GetMapping("/forecasts")
    @Operation(summary = "分页获取省会未来天气信息", description = "分页获取省会未来天气信息")
    @ApiResponse(responseCode = "200", description = "成功获取天气信息",
            content = @Content(mediaType = "application/json",
                array = @ArraySchema(
                    schema = @Schema(implementation = WeatherForecastWithCitiesVo.class)
                )
            )
    )
    public Result<PageVo<WeatherForecastWithCitiesVo>> getCapitalCitiesWeatherForecast(@Parameter(name = "page",description = "页码")
                                                                                         @RequestParam(defaultValue = "1" , required = false) int page,
                                                                                     @Parameter(name = "size",description = "每页数量")
                                                                                         @RequestParam(defaultValue = "5" , required = false) int size) {
        return Result.success(weatherService.pageCapitalCitiesWeatherForecast(page,size));
    }




    @GetMapping("/capital")
    @Operation(summary = "获取省会城市实时今日天气信息", description = "获取省会城市天气今日信息")
    @ApiResponse(responseCode = "200", description = "成功获取天气信息",
            content = @Content(mediaType = "application/json",
                array = @ArraySchema(
                    schema = @Schema(implementation = WeatherNowVo.class)
                )
            )
    )
    public Result<RealTimeDataVo<List<WeatherNowVo>>> getCapitalCitiesWeather() {
        return Result.success(weatherService.getCapitalCitiesLocalWeather());
    }


}
